package com.util.msn;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import rath.msnm.BuddyGroup;
import rath.msnm.BuddyList;
import rath.msnm.MSNMessenger;
import rath.msnm.SwitchboardSession;
import rath.msnm.UserStatus;
import rath.msnm.entity.MsnFriend;
import rath.msnm.msg.MimeMessage;
/**
 * 
 * <p>功能描述:类功能描述</p>
 * @author  <a href="mailto: zhuyoulong@gmail.com">zhuyoulong</a>
 * @create_date 2008-10-31
 * @version $ Revision: 1.0
 */
public class MSNDaemon2 extends Thread {
	
	private static MSNMessenger msn;
	public static void main(String[] args) {
	 //  msn = new MSNMessenger("yaoyao9446@126.com", "fz367du");
	 //  msn.setInitialStatus(UserStatus.ONLINE);
	 //  msn.addMsnListener(new MSNAdapter(msn));
	   //必须要登录才能获得好友列表
	 //  msn.login();
	   
		
	   
//	   System.out.println("Waiting for the response....");
//	   Runtime.getRuntime().addShutdownHook(new MSNDaemon2());
	MSNDaemon2 aa = new MSNDaemon2();
	aa.getMsnList("yaoyao9446@126.com", "fz367du");
	//aa.writeToMe(friend);

	
	}
	/**
	* 用户中止程序执行
	*/
	public void run() {
	   msn.logout();
	   System.out.println("MSN Logout OK");
	}
	
	
	public List getMsnList(String number, String password) {
		MSNMessenger msn = new MSNMessenger(number, password);
		msn.setInitialStatus(UserStatus.ONLINE);
		msn.login();
		BuddyGroup group = msn.getBuddyGroup();
		BuddyList buddyList = group.getAllowList();

		List list = new ArrayList();
		String loginName = "";
		String name="";
		MsnFriend friend = null;
		for (Iterator it = buddyList.iterator(); it.hasNext();) {
			friend = (MsnFriend) it.next();
			loginName = friend.getLoginName();
			name=friend.getFriendlyName();
			
			System.out.print("loginName22 = " + loginName+"==="+name+"\n");
			list.add(friend);

		}
		return list;
	}
	
	public void writeToMe(MsnFriend friend) {
		try {
			msn.doCall("zhu_you_long@hotmail.com");
			   Thread.sleep(1000);//最好在此有所延迟,要不SwitchboardSession创建容易失败 
				SwitchboardSession ss = msn.doCallWait("zhu_you_long@hotmail.com"); 
				MimeMessage msg = new MimeMessage(); 
				msg.setKind(MimeMessage.KIND_MESSAGE); 
				msg.setMessage("你好,周末愉快!"); 
				ss.sendInstantMessage(msg);
				System.out.print("loginName22 = " );
		   } catch (Exception e) {
		    e.printStackTrace();
		   }
		}
	
	
	

}
